package handlers

import (
	"encoding/json"
	"net/http"
	"shopback/models"
	"strconv"
	"github.com/gorilla/mux"
)

// AddAddress 添加地址
	// @Summary 添加收货地址
	// @Description 为用户添加新的收货地址
	// @Tags 地址
	// @Accept json
	// @Produce json
	// @Param address body models.Address true "地址信息"
	// @Success 201 {object} map[string]interface{} "地址添加成功"
	// @Failure 400 {string} string "请求参数错误"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /users/{user_id}/addresses [post]
func AddAddress(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var address models.Address
	if err := json.NewDecoder(r.Body).Decode(&address); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的请求数据"})
		return
	}

	addressID, err := models.AddAddress(address)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusCreated)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "地址添加成功",
		"address_id": addressID,
	})
}

// GetUserAddresses 获取用户的所有地址
	// @Summary 获取用户地址列表
	// @Description 获取指定用户的所有收货地址
	// @Tags 地址
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Success 200 {array} models.Address "地址列表"
	// @Failure 400 {string} string "无效的用户ID"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /users/{user_id}/addresses [get]
func GetUserAddresses(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	vars := mux.Vars(r)

	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的用户ID"})
		return
	}

	addresses, err := models.GetUserAddresses(userID)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(addresses)
}

// GetUserAddressByID 获取用户的具体地址
	// @Summary 获取用户具体地址
	// @Description 获取指定用户具体的收货地址
	// @Tags 地址
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Param address_id path int true "地址ID"
	// @Success 200 {object} map[string]string "地址获取成功"
	// @Failure 400 {string} string "无效的用户ID"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /users/{user_id}/addresses/{address_id} [get]
func GetUserAddressByID(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	vars := mux.Vars(r)

	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的用户ID"})
		return
	}

	address_id, err := strconv.Atoi(vars["address_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的地址ID"})
		return
	}

	address, err := models.GetUserAddressByID(userID, address_id)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(address)
}

// UpdateAddress 更新地址
	// @Summary 更新收货地址
	// @Description 更新指定的收货地址信息
	// @Tags 地址
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Param address_id path int true "地址ID"
	// @Param address body models.Address true "地址更新信息"
	// @Success 200 {object} map[string]string "地址更新成功"
	// @Failure 400 {string} string "请求参数错误"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /users/{user_id}/addresses/{address_id} [put]
func UpdateAddress(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var address models.Address
	
	if err := json.NewDecoder(r.Body).Decode(&address); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的请求数据"})
		return
	}

	if err := models.UpdateAddress(address); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]interface{}{
		"message": "地址更新成功",
		"address": address})
}

// DeleteAddress 删除地址
	// @Summary 删除收货地址
	// @Description 删除指定的收货地址
	// @Tags 地址
	// @Accept json
	// @Produce json
	// @Param user_id path int true "用户ID"
	// @Param address_id path int true "地址ID"
	// @Success 200 {object} map[string]string "地址删除成功"
	// @Failure 400 {string} string "请求参数错误"
	// @Failure 500 {string} string "服务器内部错误"
	// @Router /users/{user_id}/addresses/{address_id} [delete]
func DeleteAddress(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	vars := mux.Vars(r)

	userID, err := strconv.Atoi(vars["user_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的用户ID"})
		return
	}
	
	addressID, err := strconv.Atoi(vars["address_id"])
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]string{"error": "无效的地址ID"})
		return
	}

	if err := models.DeleteAddress(addressID, userID); err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
		return
	}

	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(map[string]string{"message": "地址删除成功"})
} 